<?php
namespace app\admin\controller;
use think\Controller;
use think\Controllers;
use think\Session;
use think\Cookie;
use think\Jump;
use think\Db;
class Commodel extends Controllers{

	public function index(){
		 $user = Session::get("user");
		if($user['power'] == 0){
			$sql = "select * from ims_xuan_zy_commission_model where uid = 0 and type = 0";
		} else {
			$sql = "select * from ims_xuan_zy_commission_model where uid = {$user['uid']} and type = 0";
		}
		$arr = DB::query($sql);
		foreach ($arr as $k => $v) {
			if($v['default'] == 1){
				$arr[$k]['default'] = "是";
			} else {
				$arr[$k]['default'] = "否";

			}
		}
		$this->assign("list",$arr);
		return $this->fetch("admin/commodel");
	}
	public function add(){
	 $user = Session::get("user");

		$id = $_REQUEST['id'];
		if($id){
			$sql = "select * from ims_xuan_zy_commission_model where id =  {$id} and type = 0";
			$info = DB::query($sql)[0];
			$this->assign("info",$info);
		}
		$sql = "select bankname as bank_name from ims_xuan_zy_bank where status = 1";
		$bank = DB::query($sql);
		if ($info) {
			$bank = [];
			$ban = json_decode($info['bank'],true);
			foreach ($ban as $k => $v) {
				$bank[$k]["bank_name"] = $k;
				$bank[$k]["money"] = $v;
				$uid =$info['uid'];
			}
		}
		$uid = $user['uid'];
		if ($uid) {
			$sql = "select com_model_id from ims_xuan_zy_commission_team where uid = {$uid}";
			$model_id =DB::query($sql)[0]['com_model_id'];
			$sql = "select bank from ims_xuan_zy_commission_model where id = {$model_id} and type = 0";
			$bank_money = json_decode(DB::query($sql)[0]['bank'],true);
			foreach ($bank as $k => $v) {
				$sql = "select commision from ims_xuan_zy_commission where bank_name = '{$v['bank_name']}' and uid = {$uid}";
				$commision = DB::query($sql)[0]['commision'];
				if($commision){
					$bank[$k]["max"] = $commision;
				} else {
					if(is_numeric($k)){

						$bank[$k]["max"] = $bank_money[$v['bank_name']]['money'];
					} else {
						$bank[$k]["max"] = $bank_money[$k]['money'];

					}
				

				}
			}
		};
		$this->assign("bank",$bank);
		return $this->fetch("admin/commodel_add");


	}
	public function bank_upd(){
		 $user = Session::get("user");
		$id = $_REQUEST['id'];
		$model_name = $_REQUEST['model_name'];	
		$bank = $_REQUEST['bank'];	
		if($user['power'] == 0){
			$uid = 0;
		} else {
			$uid = $user['uid'];
		} 
		if($id){
			DB::table("ims_xuan_zy_commission_model")->where("id",$id)->setField(["bank"=>json_encode($bank)]);
			$this->success("修改成功","admin/commodel/index");
		} else {
			DB::table("ims_xuan_zy_commission_model")->insert(["model_name"=>$model_name,"bank"=>json_encode($bank),"uid"=>$uid]);
			$this->success("添加成功","admin/commodel/index");
		}
	}
	public function default(){
	    $user = Session::get("user");
		$id = $_REQUEST['id'];
		if($user['power'] == 0){
			$uid = 0;
		} else {
			$uid = $user['uid'];
		} 
		$sql = "update ims_xuan_zy_commission_model set `default`= 1 where id = {$id} and uid = {$uid} and type = 0";
		DB::query($sql);
		$sql = "update ims_xuan_zy_commission_model set `default` = 0 where id <> {$id} and uid = {$uid} and type = 0";
		DB::query($sql);
		$this->success("修改成功","admin/commodel/index");
	}
	public function bank(){
	    $user = Session::get("user");
		$id = $_REQUEST['id'];
		$this->assign("id",$id);
		if($user['power'] == 0){
			$uid = 0;
		} else {
			$uid = $user['uid'];
		} 
		$sql = "select * from ims_xuan_zy_commission_model where uid = {$uid} and type = 0";
		$model = DB::query($sql);
		$sql = "select com_model_id from ims_xuan_zy_commission_team where uid = {$id}";
		$model_id = DB::query($sql)[0]["com_model_id"];
		$this->assign("model",$model);
		$this->assign("model_id",$model_id);
		return $this->fetch("admin/comm_bank");

	}
	public function bank_add(){
		$uid =$_REQUEST['id'];
		$model_id = $_REQUEST['model_id'];
		$sql = "update ims_xuan_zy_commission_team set com_model_id = {$model_id} where uid = {$uid}" ;
		DB::query($sql);
	    $user = Session::get("user");
		if($user['power'] == 0){
		$this->success("修改成功","admin/partner/index");			
		} else {
		$this->success("修改成功","admin/team/index");			
			$uid = $user['uid'];
		} 


	}
}